package com.jeecms.edu.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.jeecms.common.web.ResponseUtils;
import com.jeecms.edu.entity.SchoolInformation;

/**
 * 根据检索条件所有学校列表页面
 */
@WebServlet("/SchoolListPage")
public class SchoolListPage extends HttpServlet {
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		List searchlist=(List) request.getSession().getServletContext().getAttribute("schoollist");
		String exadd=(String)request.getSession().getServletContext().getAttribute("exadd");
		JSONObject jsonObject=new JSONObject();
		String result="";
		if (null!=searchlist) {
			int size=searchlist.size();	
			if (size>3) {   //增加显示数量限制
				size=3;
			}
			for (int i = 0; i < size; i++) {
				Map map=(Map) searchlist.get(i);
				String school=(String) map.get("schoolname");
				String changes="javascript:gotoDetail('"+school+"');";
				String schoolbus=(String)map.get("otherschoolbus");
				String schoolbed=(String)map.get("otherschoolbed");
				String schoolkey=(String)map.get("otherschoolkey");
				String schoolprofile=(String)map.get("schoolprofile");
				String special="";
				if (schoolbus.equals("1")) {
					schoolbus="<li class='f01'>有校车</li>";
				}else{
					schoolbus="";
				}
				if (schoolbed.equals("1") || schoolbed.equals("2")) {
					schoolbed="<li class='f02'>有住宿</li>";
				}else{
					schoolbed="";
				}
				if (schoolkey.equals("1")) {
					schoolkey="<li class='f04'>重点学校</li>";
				}else{
					schoolkey="";
				}
				if (schoolbus=="" && schoolbed=="" && schoolkey=="") {
					special="";
				}else{
					special="<h6>学校特色</h6><ul>"+schoolbus+schoolbed+schoolkey+"</ul>";
				}
				if (schoolprofile=="" || schoolprofile.equals("")) {
					special=special+"";
				}else{
					special=special+"<h3>学校简介</h3><p>"+schoolprofile+"</p></div>";
				}
				result+="<div class='school_thread'><a href="+changes+"><img src='"+map.get("schoolimgurl")+"' class='school_picture' /></a>"
						+"<h2><a href="+changes+">"+map.get("schoolname")+"</a></h2>"
						+"<h4><span class='blue'>"+map.get("schoolregion")+"</span> 地址："+map.get("schooladdress")+" 电话："+map.get("schooltel")+"</h4>"
						+"<h5><img src='../images/icon_12.gif' />"+map.get("schoolnature")+" "+map.get("schoollength")+" "+map.get("schoolgroup")+"</h5>"
						+special;
			}
			jsonObject.put("exaddkey", exadd);
			jsonObject.put("total", searchlist.size());
			jsonObject.put("result", result);
			String json = jsonObject.toJSONString();
			ResponseUtils.renderJson(response, json);
		}else{
			List list=SchoolInformation.getPageSchool("");
			int size=list.size();
			request.getSession().getServletContext().setAttribute("schoollist", searchlist);
			if (size>3) {   //增加显示数量限制
				size=3;
			}
			for (int i = 0; i < size; i++) {
				Map map=(Map) list.get(i);
				String school=(String) map.get("schoolname");
				String changes="javascript:gotoDetail('"+school+"');";
				String schoolbus=(String)map.get("otherschoolbus");
				String schoolbed=(String)map.get("otherschoolbed");
				String schoolkey=(String)map.get("otherschoolkey");
				String schoolprofile=(String)map.get("schoolprofile");
				String special="";
				if (schoolbus.equals("1")) {
					schoolbus="<li class='f01'>有校车</li>";
				}else{
					schoolbus="";
				}
				if (schoolbed.equals("1") || schoolbed.equals("2")) {
					schoolbed="<li class='f02'>有住宿</li>";
				}else{
					schoolbed="";
				}
				if (schoolkey.equals("1")) {
					schoolkey="<li class='f04'>重点学校</li>";
				}else{
					schoolkey="";
				}
				if (schoolbus=="" && schoolbed=="" && schoolkey=="") {
					special="";
				}else{
					special="<h6>学校特色</h6><ul>"+schoolbus+schoolbed+schoolkey+"</ul>";
				}
				if (schoolprofile=="" || schoolprofile.equals("")) {
					special=special+"";
				}else{
					special=special+"<h3>学校简介</h3><p>"+schoolprofile+"</p></div>";
				}
				result+="<div class='school_thread'><a href="+changes+"><img src='"+map.get("schoolimgurl")+"' class='school_picture' /></a>"
						+"<h2><a href="+changes+">"+map.get("schoolname")+"</a></h2>"
						+"<h4><span class='blue'>"+map.get("schoolregion")+"</span> 地址："+map.get("schooladdress")+" 电话："+map.get("schooltel")+"</h4>"
						+"<h5><img src='../images/icon_12.gif' />"+map.get("schoolnature")+" "+map.get("schoollength")+" "+map.get("schoolgroup")+"</h5>"
						+special;
			}
			jsonObject.put("total", list.size());
			jsonObject.put("result", result);
			String json = jsonObject.toJSONString();
			ResponseUtils.renderJson(response, json);
		}
	}

}
